/*!
   \file

   © 2011 Mark W. Gabby II

   This file is part of the Avion engine.

   The Avion engine is free software: you can redistribute it and/or modify it under the terms of
   the GNU General Public License as published by the Free Software Foundation, either version 3 of
   the License, or (at your option) any later version.

   The Avion engine is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
   without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
   the GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along with the Avion Engine.
   If not, see <http://www.gnu.org/licenses/>.
*/
#include "Components/Component.hpp"

#include "Systems/EntManager/EntManager.hpp"

/*!
   \brief
      Creates a component.

   \param OwnerHandle
      The Entity that owns this component.
*/
Component::Component( Entity * Owner )
{
   owner = Owner;

   // Not enabled yet
   enabled = false;
   queuedForDelete = false;
}
/*!
   \brief
      Adds the component to the system it belongs to and to its owner.

   \note
      Internally, this adds the component's concrete class to the list of components in its
      respective system, and  adds the component itself to the list of components in the entity
      that owns it.
*/
void Component::Register()
{
   // Call derived class register for specifics and
   // get our handle
   handle = RegisterConcreteClassWithSystem();

   // Add us to our owner's list of components
   owner->AddComponent( this );

   // We are now enabled!
   enabled = true;
}

/*!
   \brief
      Deletes a component. NEVER call delete on a component, use this function instead.
*/
void Component::Delete()
{
   if ( queuedForDelete == false )
   {
      RemoveComponentFromSystem();
      queuedForDelete = true;
   }
}
/*!
   \brief
      Returns a pointer to the enity that owns this component.
*/
Entity * Component::GetOwner() const
{
   return owner;
}
/*!
   \brief
      Disable causes the component to stop being processed or used by the system it belongs to.
*/
void Component::Disable()
{
   // Only disable if we're already enabled
   if ( enabled == true )
   {
      // Call derived class
      // disable for specifics
      DisableComponent();

      enabled = false;
   }
}
/*!
   \brief
      Enable re-enables a component after it's been disabled.
*/
void Component::Enable()
{
   // Only enable if we're already disabled
   if ( enabled == false )
   {
      // Call derived class
      // enable for specifics
      EnableComponent();

      enabled = true;
   }
}
/*!
   \brief
      Gets whether the component is enabled.
   \return
      True if the component is enabled, false if it's disabled.
*/
bool Component::IsEnabled() const
{
   return enabled;
}
/*!
   \brief
      Returns this component's handle. This function should only be used by AddComponent and
      RemoveComponent.
*/
uint32_t Component::GetHandle() const
{
   return handle;
}
/*!
   \brief
      Sets this component's owner. Should only be used by MoveComponent.
   \note
      Use Entity::GiveComponentToThisEntity instead of this function.
*/
void Component::SetOwner( Entity * NewOwner )
{
   owner = NewOwner;
}
